<?php
class Application_Model_ResultMapper
{
    protected $_dbTable;
    public static $_hashmap = array('Misaka Mikoto' => '御坂美琴', 
    'Tachibana Kanade' => '立华奏', 'Nakano Azusa' => '中野梓', 
    'Nagato Yuki' => '长门有希', 'Shana' => '夏娜', 'Gokō Ruri (Kuroneko)' => '黑猫', 
    'Eucliwood Hellscythe' => '优库里伍德', 'Akiyama Mio' => '秋山澪', 
    'Aisaka Taiga' => '逢坂大河', 'Katsura Hinagiku' => '桂雏菊', 'Yui' => '由依', 
    'Charlotte Dunois' => '夏洛特', 'Nakamura Yuri' => '仲村由理', 
    'Suzumiya Haruhi' => '凉宫春日', 'Saber' => 'Saber', 'Sengoku Nadeko' => '千石抚子', 
    'Senjōgahara Hitagi' => '战场原黑仪', 'Victorique de Blois' => '维多利加', 
    'Sakagami Tomoyo' => '坂上智代', 'Kōsaka Kirino' => '高坂桐乃', 
    'Hirasawa Yui' => '平泽唯', 'Sanzenin Nagi' => '三千院凪', 
    'Fujibayashi Kyō' => '藤林杏', 'Hiiragi Kagami' => '柊镜', 
    'Konjiki no Yami' => '金色之暗', 'Izumi Konata' => '泉此方', 
    'Furukawa Nagisa' => '古河渚', 'Aragaki Ayase' => '新垣绫濑', 
    'Asahina Mikuru' => '朝比奈实玖瑠', 'Kuroi Mato' => '黑岩射手', 'Holo' => '赫萝', 
    'C.C.' => 'C.C.', 'Index L. Prohibitorum' => '茵蒂克丝', 
    'Louise Vallière' => '露易丝', 'Kotobuki Tsumugi' => '琴吹紬', 
    'Fate Testarossa' => '菲特', 'Shiina Mafuyu' => '椎名真冬', 'Hirasawa Ui' => '平泽忧', 
    'Last Order' => '最后之作', 'Shirai Kuroko' => '白井黑子', 'Haruna' => '春奈', 
    'Tōsaka Rin' => '远坂凛', 'Iwasawa Asami' => '岩泽麻美', 'Nymph' => '尼姆芙', 
    'Shinonono Hōki' => '筱之之箒', 'Seraphim' => '瑟拉芬', 'Huang Lingyin' => '黄玲音', 
    'Kasugano Sora' => '春日野穹', 'Shiina' => '椎名', 'Laura Bodewig' => '萝拉');
    public function setDbTable ($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (! $dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }
    public function getDbTable ()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_Result');
        }
        return $this->_dbTable;
    }
    public function save (Application_Model_Result $result)
    {
        $data = array('round' => $result->getRound(), 
        'name' => $result->getName(), 'rank' => $result->getRank(), 
        'win' => $result->getWin(), 'draw' => $result->getRank(), 
        'lose' => $result->getRank(), 'sos' => $result->getRank(), 
        'sdo' => $result->getRank(), 'sao' => $result->getRank(), 
        'vf' => $result->getRank(), 'vd' => $result->getRank(), 
        'va' => $result->getRank());
        $this->getDbTable()->insert($data);
    }
    public function find ($id, Application_Model_Guestbook $guestbook)
    {
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
            return;
        }
        $row = $result->current();
        $guestbook->setId($row->id)
            ->setEmail($row->email)
            ->setComment($row->comment)
            ->setCreated($row->created);
    }
    public function getLastRound ()
    {
        $round = 1;
        $resultSet = $this->getDbTable()->fetchAll(
        $this->getDbTable()
            ->select()
            ->distinct('round')
            ->order('round DESC'));
        $count = 0;
        foreach ($resultSet as $row) {
            if ($count == 0) {
                $round = $row->round;
                $count ++;
            }
        }
        return $round;
    }
    public function getRounds ()
    {
        $rounds = array();
        $resultSet = $this->getDbTable()->fetchAll(
        $this->getDbTable()
            ->select()
            ->distinct()
            ->group('round')
            ->order('round ASC'));
        foreach ($resultSet as $row) {
            $rounds[] = $row->round;
        }
        return $rounds;
    }
    
   public function getNames ()
    {
        $names = array();
        $resultSet = $this->getDbTable()->fetchAll(
        $this->getDbTable()
            ->select()
            ->distinct()
            ->group('name')
            ->order('name ASC'));
        foreach ($resultSet as $row) {
            $names[] =self::$_hashmap[$row->name];
        }
        return $names;
    }
    
   public function getEnNames ()
    {
        $names = array();
        $resultSet = $this->getDbTable()->fetchAll(
        $this->getDbTable()
            ->select()
            ->distinct()
            ->group('name')
            ->order('name ASC'));
        foreach ($resultSet as $row) {
            $names[] =$row->name;
        }
        return $names;
    }
    
    public function fetchAll ($selected_round)
    {
        $resultSet = $this->getDbTable()->fetchAll(
        $this->getDbTable()
            ->select()
            ->where("round=?", $selected_round)
            ->order('rank ASC'));
        $entries = array();
        foreach ($resultSet as $row) {
            $entry = new Application_Model_Result();
            $entry->setName(self::$_hashmap[$row->name])
                ->setRound($row->round)
                ->setRank($row->rank)
                ->setSos($row->sos)
                ->setWin($row->win)
                ->setDraw($row->draw)
                ->setLose($row->lose)
                ->setSdo($row->sdo)
                ->setSao($row->sao)
                ->setVf($row->vf)
                ->setVd($row->vd)
                ->setVa($row->va);
            $entries[] = $entry;
        }
        return $entries;
    }
}

